Friend Class WizardDesigner
    Inherits ParentControlDesigner

    Dim Wizard As Wizard

    Private showAdornments As Boolean

    'Public Overrides ReadOnly Property SelectionRules() As System.Windows.Forms.Design.SelectionRules
    '    Get
    '        Return SelectionRules.Visible Or SelectionRules.Moveable
    '    End Get
    'End Property

    Protected Overrides Function GetHitTest(ByVal point As System.Drawing.Point) As Boolean
        point = Wizard.PointToClient(point)

        If Wizard.btnBack.Bounds.Contains(point) Then Return True
        If Wizard.btnNext.Bounds.Contains(point) Then Return True

        Return False
    End Function

    Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
        MyBase.Initialize(component)

        Wizard = DirectCast(component, Wizard)

        Dim s As ISelectionService = DirectCast(GetService(GetType(ISelectionService)), ISelectionService)
        AddHandler s.SelectionChanged, AddressOf OnSelectionChanged
    End Sub

    Private Sub OnSelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim s As ISelectionService = DirectCast(sender, ISelectionService)
        Dim p As WizardPage
        Dim newShowAdornments As Boolean = False

        'See if any of our pages are selected
        For Each p In Wizard.Pages
            If s.GetComponentSelected(p) Then newShowAdornments = True
        Next

        'If we need to do something about it
        If newShowAdornments <> showAdornments Then
            showAdornments = newShowAdornments
            Wizard.Invalidate()
        End If
    End Sub

    Public Overrides ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection
        Get
            Dim vs As New DesignerVerbCollection()

            vs.Add(New DesignerVerb("Fit Form", AddressOf OnFitForm))

            Return vs
        End Get
    End Property

    Friend Sub OnFitForm(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim c As IComponentChangeService = DirectCast(GetService(GetType(IComponentChangeService)), IComponentChangeService)

        'Relocate wizard
        c.OnComponentChanging(Wizard, Nothing)
        Wizard.Location = New Point(0, 0)
        c.OnComponentChanged(Wizard, Nothing, Nothing, Nothing)

        'Change form size
        If Not Wizard.ParentForm Is Nothing Then
            c.OnComponentChanging(Wizard.ParentForm, Nothing)
            Wizard.ParentForm.ClientSize = Wizard.Size
            Wizard.ParentForm.FormBorderStyle = FormBorderStyle.FixedDialog
            c.OnComponentChanged(Wizard.ParentForm, Nothing, Nothing, Nothing)
        End If
    End Sub

    Friend Sub OnAddPage(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim h As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
        Dim c As IComponentChangeService = DirectCast(GetService(GetType(IComponentChangeService)), IComponentChangeService)
        Dim s As ISelectionService = DirectCast(GetService(GetType(ISelectionService)), ISelectionService)
        Dim t As DesignerTransaction
        Dim p As WizardPage

        'Add page
        t = h.CreateTransaction("Add Wizard Page")
        p = DirectCast(h.CreateComponent(GetType(WizardPage)), WizardPage)
        c.OnComponentChanging(Wizard, TypeDescriptor.GetProperties(Wizard)("Pages"))
        Wizard.Pages.Add(p)
        Wizard.intSelectedPage = Wizard.Pages.IndexOf(p)
        c.OnComponentChanged(Wizard, TypeDescriptor.GetProperties(Wizard)("Pages"), Nothing, Nothing)
        t.Commit()
        Wizard.UpdatePage()

        'Select new page
        Dim a As New ArrayList()
        a.Add(p)
        s.SetSelectedComponents(a)
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        Dim s As ISelectionService = DirectCast(GetService(GetType(ISelectionService)), ISelectionService)
        RemoveHandler s.SelectionChanged, AddressOf OnSelectionChanged

        MyBase.Dispose(disposing)
    End Sub

    Protected Overrides Sub OnMouseDragEnd(ByVal cancel As Boolean)
        MyBase.OnMouseDragEnd(True)
    End Sub

    Protected Overrides Sub OnPaintAdornments(ByVal pe As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaintAdornments(pe)

        Dim wrct, wrct2 As Rectangle

        If showAdornments And Wizard.Pages.Count <> 0 Then
            wrct2 = Wizard.Pages(0).Bounds
            wrct = wrct2
            wrct.Inflate(5, 5)
            ControlPaint.DrawSelectionFrame(pe.Graphics, False, wrct, wrct2, Wizard.BackColor)
        End If
    End Sub

End Class

Friend Class WizardPageDesigner
    Inherits ParentControlDesigner

    Public Overrides ReadOnly Property SelectionRules() As System.Windows.Forms.Design.SelectionRules
        Get
            Return SelectionRules.Visible
        End Get
    End Property

    Public Overrides Function CanBeParentedTo(ByVal parentDesigner As System.ComponentModel.Design.IDesigner) As Boolean
        Return (TypeOf parentDesigner Is WizardDesigner)
    End Function

End Class
